package com.homelock.service;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.homelock.entity.HlDevInfo;
import com.homelock.entity.HlDevUser;
import com.pi.uc.dao.entity.UcUserEntity;
import com.pi.uc.service.UcUserService;

@Service
public class LockUserBindService {
    
    private LockUserService lockUserService;
    
    private HlDevInfoService devInfoService;
    
    private UcUserService userService;

    @Autowired
    public LockUserBindService(
            LockUserService lockUserService, 
            HlDevInfoService devInfoService,
            UcUserService userService) {
        this.lockUserService = lockUserService;
        this.devInfoService = devInfoService;
        this.userService = userService;
    }
    
    public boolean hasUnboundLock(Long userId) {
        UcUserEntity userInfo = userService.queryUserInfo(userId);
        if(null == userInfo || StringUtils.isBlank(userInfo.getMobile())) {
            return false;
        }
        List<HlDevUser> lockUserList = lockUserService.queryWaitBindedLock(userInfo.getMobile());
        if(CollectionUtils.isEmpty(lockUserList)) {
            return false;
        }
        List<Long> lockIdList = lockUserList.stream().map(HlDevUser::getDevId).collect(Collectors.toList());
        List<HlDevInfo> devInfoList = devInfoService.getDevInfoByIds(lockIdList);
        if(CollectionUtils.isEmpty(devInfoList)) {
            return false;
        }
        List<HlDevInfo> waitBindedLock = devInfoList.stream()
                .filter(o -> o.getSourceId().longValue() == userInfo.getSourceId()).collect(Collectors.toList());
        return !CollectionUtils.isEmpty(waitBindedLock);
    }
    
    public void bindLockUser(Long userId) {
        UcUserEntity userInfo = userService.queryUserInfo(userId);
        if(null == userInfo || StringUtils.isBlank(userInfo.getMobile())) {
            return;
        }
        List<HlDevUser> lockUserList = lockUserService.queryWaitBindedLock(userInfo.getMobile());
        if(CollectionUtils.isEmpty(lockUserList)) {
            return;
        }
        List<Long> lockIdList = lockUserList.stream().map(HlDevUser::getDevId).collect(Collectors.toList());
        List<HlDevInfo> devInfoList = devInfoService.getDevInfoByIds(lockIdList);
        if(CollectionUtils.isEmpty(devInfoList)) {
            return;
        }
        List<HlDevInfo> waitBindedLock = devInfoList.stream()
                .filter(o -> o.getSourceId().longValue() == userInfo.getSourceId()).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(waitBindedLock)) {
            return;
        }
        List<Long> waitBindedLockIdList = waitBindedLock.stream().map(HlDevInfo::getId).collect(Collectors.toList());
        List<Long> waitBindedLockUserIdList = lockUserList.stream().filter(o -> waitBindedLockIdList.contains(o.getDevId())).map(HlDevUser::getId).collect(Collectors.toList());
        lockUserService.bindUserLock(waitBindedLockUserIdList, userId);
    }
}
